package com.keon.jianbao.ui.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.AppUpdate;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.enu.WebType;
import com.keon.jianbao.ui.fragment.WebFragment;
import com.keon.jianbao.util.CommonUtils;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.TDevice;
import com.keon.jianbao.util.WechatUtils;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by keon on 2015/9/17.
 */
public class WebActivity extends BaseActivity {
    String downLoadUrl = "";

    private PopupWindow mPopupwinow = null;
    View popupView = null;

    int type = 0;
    WebType webType;

    FragmentManager fragmentManager;

    PlatformActionListener platformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            showToast("分享一下");
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            showToast("系统或网络错误");
        }

        @Override
        public void onCancel(Platform platform, int i) {
            showToast("取消操作");
        }
    };


    SimpleHandler updateHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);

            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    AppUpdate appUpdate = JsonUtils.fromJson(result.getContent(), AppUpdate.class);
                    downLoadUrl = appUpdate.getDownload();

                } else {
                    onBackPressed();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected boolean hasToolBar() {
        return WebType.getWebType(type).isHasTitle();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected int getToolBarTitle() {
        return WebType.getWebType(type).getTitle();
    }

    @Override
    public void initView() {
        fragmentManager = getSupportFragmentManager();

        initPopup();
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");
        webType = WebType.getWebType(type);
        updateHandler.setContext(this);

        FragmentTransaction ft = fragmentManager.beginTransaction();
        WebFragment webFragment = new WebFragment();
        webFragment.setArguments(bundle);
        ft.replace(R.id.webviewFragment, webFragment).commit();

        if (type == WebType.ACTIVITY_SHOW.getId() || type == webType.EXPERT_INTRO.getId()) {
            Api.apk_update(updateHandler);
        }
    }

    @Override
    public void onClick(View view) {

        Bundle bundle = getIntent().getExtras();

        int activity_id = bundle.getInt("activity_id");
        String url = webType.getUrl() + activity_id;


        int id = view.getId();
        switch (id) {
            case R.id.share_wechat:

                CommonUtils.share(this, "漏漏客户端", downLoadUrl, null, WechatMoments.NAME, platformActionListener);
                mPopupwinow.dismiss();
                break;
            case R.id.share_qq:
//                showToast("2");
                CommonUtils.share(this, "漏漏客户端", downLoadUrl, null, QQ.NAME, platformActionListener);
                mPopupwinow.dismiss();
                break;
            case R.id.share_weibo:
                CommonUtils.share(this, "漏漏客户端", downLoadUrl, null, SinaWeibo.NAME, platformActionListener);
//                CommonUtils.share(this, url, null, SinaWeibo.NAME, platformActionListener);
                mPopupwinow.dismiss();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        return super.onCreateOptionsMenu(menu);
        if (type == WebType.ACTIVITY_SHOW.getId() || type == webType.EXPERT_INTRO.getId()) {
            getMenuInflater().inflate(R.menu.menu_share, menu);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.item_share:
                mPopupwinow.showAsDropDown(toolbar, TDevice.getDisplayMetrics().widthPixels - 52, 0);
                break;
        }
        return true;
    }


    void initPopup() {
        if (mPopupwinow == null) {
            popupView = LayoutInflater.from(this).inflate(
                    R.layout.view_share, null);
            mPopupwinow = new PopupWindow(popupView,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, true);
            mPopupwinow.setBackgroundDrawable(new ColorDrawable(
                    0x00000000));

            popupView.findViewById(R.id.share_wechat).setOnClickListener(this);
            popupView.findViewById(R.id.share_qq).setOnClickListener(this);
            popupView.findViewById(R.id.share_weibo).setOnClickListener(this);
        }
    }


}
